<?php
namespace app\common\model;
use think\Model;
use think\Db;
use think\Request;

class ClubModel extends Model{

    /**
     * 创建亲友圈
     * @return
     */
    public function create_club($player_id,$club_name,$president_name,$limitation,$cid,$club_type,$area,$Senior_president,$rebate,$play_id,$room_rate){

        $data = [
            'cid' =>$cid,
            'club_name'=>$club_name,
            'club_type'=>$club_type,
            'president_id'=>$player_id,
            'president_name'=>$president_name,
            'create_time'=>date("Y-m-d H:i:s",time()),
            'limitation'=>$limitation,
            'pin_drilling_ratio'=>50,
            'area_id'=>$area,
            'play_id'=>$play_id,
            'room_rate'=>$room_rate

//            'senior_president'=>$Senior_president,
//            'rebate'=>$rebate,

        ];
        if($Senior_president != ""){
            $data['senior_president'] = $Senior_president;
            $data['rebate'] = $rebate;
        }else{
            $data['senior_president'] = null;
            $data['rebate'] = null;
        }
        if($limitation == 0){
            $data['pin_drilling_ratio'] = 50;
        }
        $club = Db::name('club')
            ->insert($data);
        if($club){
            $club_id = Db::name('club')
                ->order('create_time', 'desc')
                ->find();
            $result = Db::name('club')
                ->where("cid",$club_id['cid'])
                ->find();
            $user_club_data = [
                "player_id" =>$player_id,
                "club_id" =>$result['cid'],
                "create_time" =>date("Y-m-d H:i:s",time()),
                "status" =>1
            ];
            $club_user = Db::name('user_club')
                ->insert($user_club_data);
//                        return $club_user;

            return $club_user;
        }else{
            return $club;
        }
        return $club_user;
    }
    /**
     * 亲友圈 修改
     * @return
     */
    public function club_modify($club_id,$data,$club_diamond,$president_id){
        $club = Db::name('club')
            ->where("cid",$club_id)
            ->update($data);
        if($club !== false){
            $club_property_type = $club_id.'_'.'0';
            $club_assets_url =USER_HTTP."api/set_player_property.php";
            $club_assets_data = [
                'app_id'=>APP_ID,
                'uid'=>$president_id,
                'property_type'=>$club_property_type,
                'property_num'=>$club_diamond,
            ];
            $club_assets_data = json_encode($club_assets_data);
            $curl = new \Curl();
            $club_assets_result = $curl->vpost($club_assets_url,$club_assets_data);
            $json = json_decode($club_assets_result);
            if($json->code == 0){
                return true;
            }else{
                return false;
            }

        }else{
            return true;
        }


    }
    /**
     * 亲友圈 会长昵称查询
     * @return
     */
    public function president_name($player_id){

        $data = [
            'app_id' =>APP_ID,
            'uid' =>$player_id,

        ];
        $data = json_encode($data);
        $url =USER_HTTP."api/get_info.php";
        $curl = new \Curl();
        $result = $curl->vpost($url,$data);
        $result = json_decode($result);
        if($result->code !=0){
            return null;
        }else{
            return $result->data->nickname;
        }


    }
    /**
     * 亲友圈 会长商城配置
     * @return
     */
    public function president_configuration($data){
        $president_mall = Db::name('club_shop')
            ->insert($data);
        return $president_mall;
    }
    /**
     * 亲友圈 会长商城配置（修改）
     * @return
     */
    public function president_modify($id,$data){

        $president_mall = Db::name('club_shop')
            ->where("id",$id)
            ->update($data);
        return $president_mall;
    }
    /**
     * 亲友圈 金币商城配置
     * @return
     */
    public function club_mall_gold($data){

        $gold_mall = Db::name('gold_mall')
            ->insert($data);

        return $gold_mall;
    }
    /**
     * 亲友圈 金币商城配置(修改)
     * @return
     */
    public function gold_modify($id,$data){

        $gold_mall = Db::name('gold_mall')
            ->where("id",$id)
            ->update($data);

        return $gold_mall;

    }
    /**
     * 亲友圈 操作记录
     * @return
     */
    public function record($data){
        // $data = [
        //     'opera_time' =>$opera_time,//操作时间
        //     'opera_pepole'=>$opera_pepole,//操作人
        //     'club_name'=>$club_name,//亲友圈名
        //     'opera_type'=>$opera_type,//操作类型
        //     'player_id'=>$player_id,//创建人id
        //     'old_date'=>$old_date,//原数据
        //     'new_date'=>$new_date,//新数据

        // ];
        $club = Db::name('club_operation')
            ->insert($data);
        return $club;
    }
    /**
     * 商城 操作记录
     * @return
     */
    public function mall_record($opera_data,$start_time,$end_time){
        if($start_time !="" && $end_time !=""){
            $opera_data['start_time'] = $start_time;
            $opera_data['end_time'] = $end_time;
        }
        $opera_result = Db::name("mall_opera")
            ->insert($opera_data);
        return $opera_result;
    }
    /**
     * 自动下架
     * @return
     */
    public function Automatic_lower_frame(){
        $date = date("Y-m-d H:i:s",time());
        $result = Db::name('president_mall')
            ->where("status",1)
            ->select();
        foreach ($result as $k => $v){
            $end_time = $v['end_time'];
            if($end_time !=""){
                if($end_time < $date){
                    Db::name('president_mall')
                        ->where("id",$v['id'])
                        ->update(["status"=>0]);
                }
            }
        }
    }
    /**
     * 获取用户金币数
     * @return
     */
    public function gold($player_id,$club_id,$type){
        $assets_url =USER_HTTP."api/get_player_property.php";
        $assets_data = [
            'app_id'=>APP_ID,
            'uid'=>$player_id,
        ];
        $assets_data = json_encode($assets_data);
        $curl = new \Curl();
        $assets_result = $curl->vpost($assets_url,$assets_data);
        $assets_obj = json_decode($assets_result);
        $gold = 0;
        $gold_arr = [];
        foreach ($assets_obj->data as $key => $value) {
            $str = $value->property_type;
            $array=explode('_', $str);
            if($array[0] == $club_id){
                if($array[1] == $type){
                    $gold_arr[] =$value;
                }else{
                    $gold_arr[] =[];
                }
            }else{
                $gold_arr[] =[];
            }
        }

        $gold_arr = array_filter($gold_arr);
        if(count($gold_arr)>=1){
            foreach ($gold_arr as $g_k => $g_v){
                if(!empty($g_v)){
                    $gold = 0;
                    $gold+= $g_v->property_num+$g_v->freeze_num;
                }
            }
        }else{
            $gold = 0;
        }
        return $gold;
    }
    /**
     * 获取用户钻石数
     * @return
     */
    public function diamond($player_id,$club_id){
        $assets_url =USER_HTTP."api/get_player_property.php";
        $assets_data = [
            'app_id'=>APP_ID,
            'uid'=>$player_id,
        ];
        $assets_data = json_encode($assets_data);
        $curl = new \Curl();
        $assets_result = $curl->vpost($assets_url,$assets_data);
        $assets_obj = json_decode($assets_result);
        $diamond =0;
        $diamond_arr=[];
        foreach ($assets_obj->data as $key => $value) {
            $str = $value->property_type;
            $array=explode('_', $str);
            if($array[0] == $club_id){
                if($array[1] ==0){
                    $diamond_arr[] =$value;
                }else{
                    $diamond_arr[] = [];
                }
            }else{
                $diamond_arr[] = [];
            }
        }
        $diamond_arr = array_filter($diamond_arr);
        if(count($diamond_arr)>=1){
            foreach ($diamond_arr as $d_k=> $d_v){
                if(!empty($d_v)){
                    $diamond+= $d_v->property_num+$d_v->freeze_num;
                }
            }
        }else{
            $diamond = 0;
        }
        return $diamond;
    }
    //报数据
    public function send($event,$send_data){
        $app_id = APP_ID;
        $identity = 'root';
        $ip = IP;
        $is_debug = false;
        $Send = new \BeeSender($app_id,$identity,$ip,$is_debug);
        $send_data['server_id'] = '-';//区服id ，服务器为服务器的网元id（可传减号）
        $send_data['role_id'] = '-'.'_'.$send_data['user_id'];//角色id，若没有即为serverid_userid
        $send_data['client_id'] = '-';//设备的uuid （可传减号）
        $send_data['client_type'] = '-';//设备端应用类型：CLIENT_COCOS,CLIENT_UNITY,CLIENT_H5（可传减号）
        $send_data['ip'] = request()->ip();//事件发生端的ip
        $Send->send($event,$send_data);
    }
    /**
     * 黄钻数
     * @return
     */
    public function yellow_diamond($player_id,$club_id){
//        $player_id = Session::get("pid");
//        $club_id = input("param.club_id ");
        $type = 10003;
        $property_type = $club_id.'_'.$player_id.'_'.$type;
        //获取用户资产
        $assets_url =USER_HTTP."api/get_player_property.php";
        $assets_data = [
            'app_id'=>APP_ID,
            'uid'=>$player_id,
            // 'property_type'=>$property_type
        ];
        $assets_data = json_encode($assets_data);
        $curl = new \Curl();
        $assets_result = $curl->vpost($assets_url,$assets_data);
        $assets_obj = json_decode($assets_result);
        $drill = [];
        $yellow = 0;//赠送钻石
//        print_r($assets_obj);die;
        if($assets_obj->code == 0){
            foreach ($assets_obj->data as $key => $value) {
                array_push($drill,(array)$value);
            }
            foreach ($drill as $k =>$v){
                if($v['property_type'] ==  $property_type){
                    $yellow += $v['property_num'];
                }
            }
        }

        return $yellow;

    }
    //余额
    public function Balance($player_id){
//        $player_id = Session::get("pid");
        $type = 10009;
        //获取用户资产
        $assets_url =USER_HTTP."api/get_player_property.php";
        $assets_data = [
            'app_id'=>APP_ID,
            'uid'=>$player_id,
            // 'property_type'=>$property_type
        ];
        $assets_data = json_encode($assets_data);
        $curl = new \Curl();
        $assets_result = $curl->vpost($assets_url,$assets_data);
        $assets_obj = json_decode($assets_result);
        $drill = [];
        $Balance = 0;//赠送钻石
        foreach ($assets_obj->data as $key => $value) {
            array_push($drill,(array)$value);
        }
        foreach ($drill as $k =>$v){
            if($v['property_type'] ==  $type){
                $Balance += $v['property_num'];
            }
        }
        if($Balance !=""){
            $Balance = $Balance/100;
        }
        return $Balance;

    }
    //俱乐部耗钻
    public function club_dimond($club_id){
        $json = array('diamond_club_detail','room_club_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>'si_sheng_ma_jiang_tian_jin_ma_jiang',
            'start_date'=>null,
            'end_date'=>null,
            'club_id'=>json_encode([$club_id]),
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $r = $curl->vget($url);
        $output = json_decode($r,true);
        $sum = 0;
        $reduce_pay_gold_num =0;
        $reduce_diamond_num  = 0;
        if(count($output['result'])>0){
            foreach ($output['result'] as $key=>$value){
                if($value['refer'] == 'room_club_detail'){
                    if(isset($value['reduce_pay_gold_num'])){
                        $reduce_pay_gold_num  += $value['reduce_pay_gold_num'];//消耗充值蓝钻数
                    }
                }
                if($value['refer'] == 'diamond_club_detail'){
                    if(isset($value['reduce_diamond_num'])){
                        $reduce_diamond_num  += $value['reduce_diamond_num'];// 黄钻消耗数量(消耗)
                    }
                }
            }
            $sum +=  $reduce_diamond_num+$reduce_pay_gold_num;
        }
//        print_r($output['result']);die;

//        print_r($reduce_pay_gold_num);
        return $sum;

    }
}


?>